<template>
  <div class="app-container">
    <div v-if="editorVisible" class="wf-edior">
      <el-dialog :visible.sync="editorVisible" fullscreen>
        <addpage ref="addpage" @closeAddPage="closeAddPage"></addpage>
      </el-dialog>
    </div>

    <div class="head-container">
      <el-button
        type="success"
        icon="el-icon-search"
        size="mini"
        @click="listPage(true)"
      >
        刷新
      </el-button>
      <el-button
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="addModules"
        >新增模块
      </el-button>
    </div>

    <el-table
      ref="genTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :style="{ width: '100%', height: tableHeight }"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="50" label="序号">
        <template slot-scope="scope">
          <span>{{
            listQuery.pageNo * listQuery.pageSize + scope.row.listIndex + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="模块名称" min-width="120" />
      <el-table-column prop="modulesKey" label="流程标识" min-width="120" />
      <el-table-column prop="modulesDesc" label="模块描述" min-width="120" />
      <el-table-column prop="createTime" label="创建时间" min-width="120" />
      <el-table-column prop="updateTime" label="更新时间" min-width="120" />
      <el-table-column prop="status" label="模块状态" min-width="50">
        <template slot-scope="scope">
          <el-switch
            @change="handleStatus(scope.row)"
            v-model="scope.row.status"
            active-value="show"
            inactive-value="hidden"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column align="left" label="操作" min-width="280">
        <template slot-scope="scope">
          <el-button
            style="margin-left: 10px"
            type="success"
            size="mini"
            @click="handleUpdate(scope.row)"
          >
            编辑模块
          </el-button>
          <el-button
            style="margin-left: 10px"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
          <el-button
            style="margin-left: 10px"
            type="primary"
            size="mini"
            @click="handleUpload(scope.row)"
          >导入
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      style="margin-top: 8px"
      layout="total, prev, pager, next"
      :page-size="listQuery.pageSize"
      :total="total"
      prev-text="上一页"
      next-text="下一页"
      :current-page="listQuery.pageNo + 1"
      @current-change="paginationChange"
    >
    </el-pagination>

    <input ref="file" type="file" style="display: none" @change="uploadProcessFile">
  </div>
</template>

<script>
import { del, listPage, findOne, add } from "@/api/modules/index";
import msgUtil from "@/utils/msgUtil";
import addpage from "./addpage";
import {importActiviti} from '@/api/wf/modeler'

export default {
  components: {
    addpage,
  },
  data() {
    return {
      tableHeight: window.innerHeight - 270 + "px",
      editorVisible: false,
      list: null,
      listLoading: false,
      total: 0,
      listQuery: {
        pageNo: 0,
        pageSize: 10,
        condition: "",
      },
      itemId: ''
    };
  },
  created() {
    this.listPage();
  },
  methods: {
    //新增
    addModules() {
      //清除按钮缓存
      this.$store.state.activiti.btns = {};
       //清空编辑的缓存数据
      this.$store.state.activiti.editData = [];
      this.editorVisible = true;
      this.$nextTick(() => {
        this.$refs.addpage.form = { id: "" };
      });
    },
    //编辑模块
    handleUpdate(row) {
      //清空编辑的缓存数据
      this.$store.state.activiti.editData = [];
      this.editorVisible = true;
      findOne(row.id).then((res) => {
        this.$refs.addpage.form = res.data;
        this.$refs.addpage.initData();
      });
    },

    //关闭新增窗口
    closeAddPage() {
      this.editorVisible = false;
      msgUtil.success2("提交成功!");
      this.listPage(true);
    },
    paginationChange(index) {
      this.listQuery.pageNo = index;
      this.listPage();
    },
    handleUpload(obj) {
      this.itemId = obj.modulesId
      const e = this.$refs.file
      e.click()
    },
    uploadProcessFile(e) {
      const formData = new FormData()
      const file = e.target.files[0]
      formData.append('file', file)
      formData.append("id", this.itemId);
      importActiviti(
        formData,
        {headers: {'Content-Type': 'multipart/form-data;boundary=' + new Date().getTime()}}
      ).then((res) => {
        msgUtil.success2("导入成功!");
      })
    },
    // 删除
    handleDelete(row) {
      msgUtil.confirm("确认删除表数据?").then(async () => {
        del(row).then((res) => {
          if (res.code == 200) {
            msgUtil.success2("删除成功!");
            this.listPage();
          }
        });
      });
    },
    listPage(reload) {
      if (reload) {
        this.listQuery.pageNo = 0;
      }
      this.listLoading = true;
      listPage(this.listQuery).then((res) => {
        this.list = res.data.content;
        this.total = res.data.totalElements;
        res.data.content.forEach((item, index) => {
          item.listIndex = index;
        });
        this.$nextTick(() => {
          this.listLoading = false;
        });
      });
    },

    // 启用状态
    handleStatus(row) {
      add(row).then((response) => {});
    },
  },
};
</script>

<style lang="scss" scoped>


</style>
